//# sourceURL=http://uMapApp/Module/Bridge/BridgeDetectionModule
define([
    "mhjs/mhjs"
    , "uMap/uMap"
    , "uEarth/uEarth"
    , "../Module"
    , "../../UI/Panel/FloatPanel"
    , "../../UI/Panel/PanelBlock/ListBlock"
    , "../../UI/Panel/PanelBlock/PropertiesBlock"
    , "../../UI/Panel/PanelBlock/ChartBlock"
    , "../../UI/Panel/PanelBlock/TableBlock"
    , "../../UI/Panel/Item/ListItem"
    , "./BridgeDetection"
], function (
    mhjs
    , uMap
    , uEarth
    , Module
    , FloatPanel
    , ListBlock
    , PropertiesBlock
    , ChartBlock
    , TableBlock
    , ListItem
    , BridgeDetection
) {

    function BridgeDetectionModule(option) {

        var _this = this;

        Module.call(_this, option);

        _this.title = "桥梁监测";
        _this.icon = _this.app.icons.bridge;
        _this.panels = [];
        _this.items = [];

        _this.listPanel = new FloatPanel({"class": "rightPanel", "showClose": true});
        _this.listPanel.listBlock = new ListBlock({"class": "fullBlock"});
        _this.listPanel.listBlock.title = "桥梁监测";
        _this.listPanel.append(_this.listPanel.listBlock);

        _this.listPanel.on("close", function () {
            _this.closePanel();
        });

        _this.panels.push(_this.listPanel);

        _this.chartPanel = new FloatPanel({"class": "leftPanel BridgeDetectionModule chartPanel"});
        _this.chartPanel.chartBlock = new ChartBlock({
            "title": _this.title,
            "chart": {
                "height"     : 270,
                "width"      : 740,
                "chartOption": {
                    xAxis  : {
                        type       : "category",
                        boundaryGap: false
                    },
                    yAxis  : {
                        type: "value"
                    },
                    series : {
                        type: "line"
                    },
                    grid   : {
                        top         : "0%",
                        left        : "0%",
                        right       : "0%",
                        bottom      : "0%",
                        containLabel: true
                    },
                    tooltip: {
                        trigger        : "item",
                        backgroundColor: "rgb(1, 20, 30)",
                        borderColor    : "#333",
                        textStyle      : {
                            color: "#fff"
                        },
                        confine        : true
                    }
                }
            }
        });

        _this.chartPanel.tableBlock = new TableBlock();

        _this.chartPanel.tableBlock.head = [
            {
                "label" : "#",
                "col"   : {"width": 50},
                "format": function (tableBlock, td) {
                    td.text(tableBlock.body.length + 1);
                }
            },
            {
                "value": "data",
                "label": "数值"
            },
            {
                "value": "time",
                "label": "更新时间"
            }
        ];

        _this.chartPanel.tableBlock.ui.title.hide();
        _this.chartPanel.tableBlock.ui.filterBlock.hide();

        _this.chartPanel
            .append(_this.chartPanel.chartBlock)
            .append(_this.chartPanel.tableBlock)
        ;

        _this.panels.push(_this.chartPanel);

        // _this.detailPanel = new FloatPanel({"class": "leftPanel", "showClose": true});
        // _this.detailPanel.propertiesBlock = new PropertiesBlock({"class": "fullBlock"});
        // _this.detailPanel.append(_this.detailPanel.propertiesBlock);
        //
        // _this.detailPanel.on("open", function () {
        //     _this.trigger("openDetail");
        // });
        //
        // _this.detailPanel.on("close", function () {
        //     _this.trigger("closeDetail");
        //     // _this.unActiveAllItem();
        // });
        //
        // _this.panels.push(_this.detailPanel);

        _this.app.ui.layout.right.append(_this.listPanel);
        _this.app.ui.layout.left.append(_this.chartPanel);
        // _this.app.ui.layout.left.append(_this.detailPanel);
    }

    var _class = BridgeDetectionModule;

    mhjs.Inherit(_class, Module);

    Object.defineProperties(_class.prototype, {
        "data"           : {
            set: function (data) {
                var _this = this;
                _this.clear();
                _this._data = data;
                data && _this.renderList(data);
            },
            get: function () {
                return this._data;
            }
        },
        "renderList"     : {
            value: function (list) {

                var _this = this;

                _this.bridges = {};

                $.each(list, function (i, item) {

                    if (item.bridgeNum && _this.app.modules.road.bridgeModule.bridges[item.bridgeNum]) {
                        item.bridge = _this.app.modules.road.bridgeModule.bridges[item.bridgeNum];
                    }

                    if (_this.objMap) {
                        item = _this.objMap.format(item);
                    }

                    item.app = _this.app;


                    var bridgeDetection = new BridgeDetection(item);

                    _this.items.push(bridgeDetection);

                    bridgeDetection.dataRequest = _this.dataRequest;

                    bridgeDetection.listItem = new ListItem({
                        "title"   : bridgeDetection.title,
                        "subtitle": bridgeDetection["节点类型"],
                        "icon"    : _this.app.icons.bridge.base64,
                        "bridge"  : bridgeDetection
                    });

                    bridgeDetection.on("click", function () {
                        _this.activeItem(bridgeDetection);
                    });

                    // bridgeDetection.open();

                    bridgeDetection.on("active", function (bridgeDetection) {
                        bridgeDetection.loadData(function () {
                            _this.chartPanel.open(false);
                            _this.chartPanel.chartBlock.title = bridgeDetection.title;

                            _this.chartPanel.tableBlock.clear();


                            var date = [];
                            var data = [];

                            $.each(bridgeDetection.data, function (i, dataItem) {
                                var dataTime = new Date(dataItem.dataObj.m_ts * 1000);
                                date.push(dataTime.format("MM-dd hh:mm:ss"));
                                data.push(dataItem.dataObj.main_monitor_data);

                                _this.chartPanel.tableBlock.add({
                                    "data": dataItem.dataObj.main_monitor_data,
                                    "time": dataTime.format("MM-dd hh:mm:ss")
                                });

                            });


                            _this.chartPanel.chartBlock.chart.setOption({
                                xAxis : {
                                    data: date
                                },
                                series: [{
                                    data: data
                                }]
                            });
                        });
                    });

                    bridgeDetection.listItem.on("click", function () {
                        _this.activeItem(bridgeDetection);
                        bridgeDetection.layer.active();
                        // _this.detailPanel.propertiesBlock.title = bridgeDetection["名称"];
                        // _this.detailPanel.propertiesBlock.map = _this.propertiesMap;
                        // _this.detailPanel.propertiesBlock.properties = bridgeDetection;
                        // _this.detailPanel.open();

                    });

                    bridgeDetection.listItem.on("hide", function (listItem) {
                        listItem.bridge.close();
                    });

                    bridgeDetection.listItem.on("show", function (listItem) {
                        listItem.bridge.open();
                    });

                    _this.listPanel.listBlock.add(bridgeDetection.listItem);

                });

                _this.listPanel.listBlock.filter = {
                    "param": "bridgeDetection.searchKey"
                };

                _this.listPanel.listBlock.sorter = {
                    "param": {
                        "value": "bridgeDetection.sortKey",
                        "label": "节点ID"
                    }
                };
            }
        },
        "clear"          : {
            value: function () {
                var _this = this;

                $.each(_this.items, function (i, item) {
                    item.close();
                    item.listItem.remove();
                });

                _this._data = undefined;
                _this.items = [];
            }
        },
        "open"           : {
            value: function (forceReload) {
                var _this = this;

                _this.loadInit(function () {
                    _this.isOpened = true;
                    $.each(_this.items, function (i, item) {
                        item.open();
                    });

                    _this.listPanel.open(false);

                    _this.trigger("open");
                    _this.trigger("toggle");
                }, forceReload);


            }
        },
        "loadInit"       : {
            value: function (callback, forceReload) {

                var _this = this;

                if (forceReload || !_this.isLoaded) {

                    _this.load({}, function (data, mapData) {

                        _this.app.modules.road.bridgeModule.loadInit(function () {

                            if (mapData) {
                                _this.objMap = mapData.obj;
                                _this.propertiesMap = mapData.properties;
                            }

                            _this.data = data;
                            _this.isLoaded = true;
                            callback && callback();

                        });


                    });

                } else {
                    callback && callback();
                }


            }
        },
        "openPanel"      : {
            value: function () {
                var _this = this;
                if (!_this.isOpened) {
                    _this.open();
                }

                _this.listPanel.open();
                _this.isPanelOpened = true;
                _this.trigger("openPanel");

            }
        },
        "close"          : {
            value: function () {
                var _this = this;
                _this.closePanel();
                $.each(_this.items, function (i, item) {
                    item.close();
                });
                _this.isOpened = false;
                _this.trigger("close");
                _this.trigger("toggle");
            }
        },
        "closePanel"     : {
            value: function () {
                var _this = this;
                _this.closeAllPanel();
                _this.isPanelOpened = false;
                _this.trigger("closePanel");
            }
        },
        "activeItem"     : {
            value: function (item) {
                var _this = this;
                _this.unActiveAllItem();
                item.active(!item.isActive);
                item.listItem.toggleClass("active", item.isActive);
            }
        },
        "unActiveAllItem": {
            value: function () {
                var _this = this;
                $.each(_this.items, function (i, item) {
                    item.active(false);
                    item.listItem.removeClass("active");
                });
            }
        }
    });

    return _class;

});